home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / DTSCPlusLibrary / Sources / Volume.cp < prev    next >
Encoding:
Text File  |  1993-01-14  |  4.6 KB  |  223 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright © 1993 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Programmer: Kent Sandvik
  5.   Date: 1/5/93
  6.   Revision comments are at the end of this file.
  7.   ---
  8.   TVolume is a simple volume based utility class (provides information about volumes)     
  9.   TVolume.cp contains the TVolume member functions. 
  10.   _________________________________________________________________________________________________________ */
  11.  
  12. #ifndef _VOLUME_
  13. #include "Volume.h"
  14. #endif
  15.  
  16. // CONSTRUCTORS & DESTRUCTORS
  17. #pragma segment Volume
  18. TVolume::TVolume()
  19. {
  20.     // Assume we will start with the default boot volume.
  21.     fVRefNum = this->GetBootVRefNum();
  22.     fIndex = 1;                                    // boot block index
  23. }
  24.  
  25.  
  26. #pragma segment Volume
  27. TVolume::TVolume(short refNum)
  28. {
  29.     // Use specified refNum.
  30.     fVRefNum = refNum;
  31. }
  32.  
  33.  
  34. #pragma segment Volume
  35. TVolume::~TVolume()
  36. {
  37. }
  38.  
  39.  
  40. // MAIN INTERFACE
  41. #pragma segment Volume
  42. short TVolume::GetBootVRefNum()
  43. // Return out the VRefNum of the boot volume.
  44. {
  45.     short ourVRefNum = 0;
  46.     long ourDirID = 0;
  47.  
  48.     fError = ::FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &ourVRefNum, &ourDirID);
  49.     VASSERT(fError == noErr, ("Problems with FindFolder = %d", fError));
  50.  
  51.     return ourVRefNum;
  52. }
  53.  
  54.  
  55. #pragma segment Volume
  56. long TVolume::GetKFreeSpace()
  57. // Get amount of free space (Kb) on defined volume.
  58. {
  59.     if (this->GetHParamBlock())
  60.         return ((fPB.volumeParam.ioVFrBlk * fPB.volumeParam.ioVAlBlkSiz) / 1024);
  61.     else
  62.         return -1;
  63. }
  64.  
  65.  
  66. #pragma segment Volume
  67. long TVolume::GetFileCount()
  68. // Return amount of files on volume.
  69. {
  70.     if (this->GetHParamBlock())
  71.         return fPB.volumeParam.ioVFilCnt;
  72.     else
  73.         return -1;
  74. }
  75.  
  76.  
  77. #pragma segment Volume
  78. long TVolume::GetDirCount()
  79. // Return amount of folders on volume.
  80. {
  81.     if (this->GetHParamBlock())
  82.         return fPB.volumeParam.ioVDirCnt;
  83.     else
  84.         return -1;
  85. }
  86.  
  87.  
  88. #pragma segment Volume
  89. long TVolume::GetCreationDate()
  90. // Return creation date of volume.
  91. {
  92.     if (this->GetHParamBlock())
  93.         return fPB.volumeParam.ioVCrDate;
  94.     else
  95.         return -1;
  96. }
  97.  
  98.  
  99. #pragma segment Volume
  100. short TVolume::GetDriverNumber()
  101. // Get specified driver number.
  102. {
  103.     if (this->GetHParamBlock())
  104.         return fPB.volumeParam.ioVDrvInfo;
  105.     else
  106.         return 0;
  107. }
  108.  
  109.  
  110. #pragma segment Volume
  111. Boolean TVolume::GetName(Str255& name)
  112. // Get name of volume.
  113. {
  114.  
  115.     fPB.volumeParam.ioNamePtr = (StringPtr)name;
  116.     fPB.volumeParam.ioVRefNum = fVRefNum;
  117.     fPB.volumeParam.ioVolIndex = fIndex;
  118.  
  119.     fError = ::PBHGetVInfoSync(&fPB);
  120.     VASSERT(fError == noErr, ("Problems with PBHGetVInfo = %d", fError));
  121.  
  122.     return (fError == noErr);
  123. }
  124.  
  125.  
  126. #pragma segment Volume
  127. short TVolume::GetNumVolumes()
  128. // Get the amount of mounted volumes (at this instance).
  129. {
  130.     short num = 0;
  131.  
  132.     for (this->Reset(); !this->Last(); this->Next())
  133.         num++;
  134.  
  135.     return num;
  136. }
  137.  
  138.  
  139. // GET/SET FUNCTIONS
  140. #pragma segment Volume
  141. void TVolume::SetVRefNum(const short refNum)
  142. // Set used VRefNum.
  143. {
  144.     fVRefNum = refNum;
  145. }
  146.  
  147.  
  148. #pragma segment Volume
  149. short TVolume::GetVRefNum() const
  150. // Get used VRefNum.
  151. {
  152.     return fVRefNum;
  153. }
  154.  
  155.  
  156. // ITERATORS
  157. #pragma segment Volume
  158. void TVolume::Reset()
  159. // Get back to boot block references.
  160. {
  161.     fVRefNum = this->GetBootVRefNum();            // boot block VRefNum
  162.     fIndex = 1;                                    // boot block index
  163.     fLast = false;                                // don't know that yet
  164.     this->Next();                                // step once to the right position (boot drive)
  165. }
  166.  
  167.  
  168. #pragma segment Volume
  169. Boolean TVolume::Last()
  170. // Return boolan if this is the last volume or not.
  171. {
  172.     return fLast;
  173. }
  174.  
  175.  
  176. #pragma segment Volume
  177. void TVolume::Next()
  178. // Iterate to next volume mounted.
  179. {
  180.     if (!fLast)
  181.     {
  182.         fPB.volumeParam.ioNamePtr = NULL;
  183.         fPB.volumeParam.ioVRefNum = fVRefNum;
  184.         fPB.volumeParam.ioVolIndex = fIndex;
  185.  
  186.         fError = ::PBHGetVInfoSync(&fPB);
  187.  
  188.         if (fError != nsvErr)                    // "No such volume" indicates that we are the end
  189.         {
  190.             fIndex++;                            // OK, increase the index
  191.             fVRefNum = fPB.volumeParam.ioVRefNum;// keep track of the VRefNum
  192.         }
  193.         else
  194.             fLast = true;                        // we hit the end, raise the flag
  195.     }
  196. }
  197.  
  198.  
  199. // INTERNAL FUNCTIONS
  200. #pragma segment Volume
  201. Boolean TVolume::GetHParamBlock()
  202. // Generate a lookup in a pre-defined param block (used by other member functions).
  203. {
  204.     fPB.volumeParam.ioNamePtr = NULL;
  205.     fPB.volumeParam.ioVRefNum = fVRefNum;
  206.     fPB.volumeParam.ioVolIndex = 0;
  207.  
  208.     fError = ::PBHGetVInfoSync(&fPB);
  209.     VASSERT(fError == noErr, ("Problems with PBHGetVInfo = %d", fError));
  210.  
  211.     return (fError == noErr);
  212. }
  213.  
  214.  
  215. // _________________________________________________________________________________________________________ //
  216.  
  217.  
  218. /*    Change History (most recent last):
  219.   No        Init.    Date        Comment
  220.   1            khs        1/5/93        New file
  221.   2            khs        1/7/93        Cleanup
  222. */
  223.